package com.mjh.zhujianju.security;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.BeanUtils;

import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.UserService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.pojo.LoginUserInfo;
import com.livew.framework.utils.XTCurrentUser;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.model.set.PermissionEO;
import com.mjh.zhujianju.service.set.PermissionUserService;
import com.mjh.zhujianju.util.ConstantUtils;

/**
 * 用户身份验证,授权 Realm 组件
 * 
 **/
//@Component(value = "securityRealm")
public class SecurityRealm extends AuthorizingRealm {

	@Resource
	private UserService userService;
	
	@Resource
	private SysUserService sysUserService;
	
	@Resource
	private PermissionUserService permissionUserService ;
	
	@Resource
	private FileService fileService;
	
	/**
	 * 权限检查
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		List<PermissionEO>  list =  permissionUserService.findPermissionByUserId(XTCurrentUser.getCurrentUserId());
		 List<String> permissions=new ArrayList<>();
		 if(!XTListUtils.isEmpty(list)) {
			 for (PermissionEO p : list) {
				 permissions.add(p.getMenuLink());
			}
			 authorizationInfo.addStringPermissions(permissions);
		 }
		return authorizationInfo;
	}

	/**
	 * 登录验证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String username = String.valueOf(token.getPrincipal());
		String password = new String((char[]) token.getCredentials());
		SysUser user = null;
		try {
			user =this.userService.getLoginNameAndPassword(username, password);
		if (user == null ||!user.getPassword().equals(password)) {
			throw new AuthenticationException("用户名或密码错误.");
		}
		} catch (Exception e) {
			e.printStackTrace();
			throw new AuthenticationException(e.getMessage());
		}
		LoginUserInfo vo = new LoginUserInfo();
		BeanUtils.copyProperties(user, vo);
		vo.setUserType(user.getType());
		try {
			SysUser user2 = sysUserService.getByPrimaryKey(user.getUserId());
			if(user2!=null) {
				vo.setPhone(user2.getPhone());
				//头像
				List<Long> files =	fileService.getFileByCaseId(user.getId(), SysUser.class.getName(), null);
				if(!XTListUtils.isEmpty(files)) {
					vo.setHeadImage("/file/showImage/"+files.get(0));
				}else {
						vo.setHeadImage("/image/defualt.jpg");
				}
			}
		} catch (XTBusinessException e) {
			e.printStackTrace();
			throw new AuthenticationException(e.getMessage());
		}
		SecurityUtils.getSubject().getSession().setAttribute(ConstantUtils.LOGIN_USER_INFO, vo);
		SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, getName());
		return authenticationInfo;
	}
}
